using GalaSoft.MvvmLight;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace RssReader.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public ObservableCollection<Entry> entry = new ObservableCollection<Entry>();
        public MainViewModel()
        {
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real"
            ////}
            LoadData();
        }
        public async void LoadData()
        {
            //StreamReader myFile = new StreamReader(@"json.txt");
            //string myString = myFile.ReadToEnd();
            //RootObject feed = JsonConvert.DeserializeObject<RootObject>(myString);
            //entry = feed.entries;
            ObservableCollection<Entry>  temp = (await DownloadPageAsync()).entries;
            foreach (var item in temp)
            {
                entry.Add(item);
            } 
        }
        private async Task<RootObject> DownloadPageAsync()
        {

            string page = "https://www.facebook.com/feeds/notes.php?id=169952729703512&viewer=100000103724778&key=AWg34koXay1j5ZC9&format=json";

            var client = new HttpClient();
            var data = await client.GetStringAsync(page);
            string result = data.ToString();


            RootObject feed = JsonConvert.DeserializeObject<RootObject>(result);
            return feed;
        }
    }
}